package com.freewater.springboot.demo.resource.protocolresolver;

import com.freewater.springboot.demo.PackageUtils;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

@Slf4j
public class CustomProtocolResolver implements ProtocolResolver {

    public static final String PATH = "path:";

    @Override
    public Resource resolve(String location, @NotNull ResourceLoader resourceLoader) {
        if (!location.startsWith(PATH)) {
            return null;
        }
        String realPath = location.substring(PATH.length());
        String classPath = "classpath:" + PackageUtils.getClassPathFroClass(this.getClass()) + "/" + realPath;
        log.info("use path to find resource: {}", classPath);
        return resourceLoader.getResource(classPath);
    }
}
